package me.zhengjie.search.service;

import me.zhengjie.search.SearchAutocompleteDTO;
import me.zhengjie.search.mapper.SearchMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SearchService {

    @Autowired
    private SearchMapper searchMapper;
    /**
     * 根据关键词查询联想词条，最多返回 10 条
     * 当前版本只从 title 查询，后续可扩展 author_name、content 等
     */
    public List<SearchAutocompleteDTO> findSuggestions(String keyword) {
        // 从 title 中查
        List<String> titles = searchMapper.findTitles(keyword);

        // 如果需要，也可以再 union author_name, content 结果：
        // List<String> authors  = searchMapper.findAuthorNames(keyword);
        // List<String> snippets = searchMapper.findContentSnippets(keyword);
        // 然后合并、去重、截取前 10 条...

        return titles.stream()
                .map(SearchAutocompleteDTO::new)
                .collect(Collectors.toList());
    }
}

